﻿using System.Linq;
using System.Windows;
using personalplaner.business.common;
using personalplaner.common.mvvm;
using personalplaner.servicemodel.dataaccess;
using System.Windows.Media;
using personalplaner.gui.common;
using personalplaner.business;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using personalplaner.common.mvvm.commands;
using personalplaner.business.enums;

namespace personalplaner.gui.settings.viewmodels
{
	public class SettingsViewModel : BaseViewModel
	{
		public SettingsViewModel(DependencyObject parent)
            : base(parent)
        {
        }

        public override void Loaded()
        {
            var mw = GetService<IMainWindowDisplayContainer>();
            mw.IsWaitVisible = false;
        }

		#region DisplaySettings

		public bool ShowDaysInPlan
		{
			get
			{
				var da = GetService<IOptionsDataAccess>();
				return da.ShowDaysInPlan;
			}
			set
			{
				var da = GetService<IOptionsDataAccess>();
				da.ShowDaysInPlan = value;
				Mediator.NotifyColleagues("SettingsRefresh");
			}
		}

		public bool ShowPensumInPlan
		{
			get
			{
				var da = GetService<IOptionsDataAccess>();
				return da.ShowPensumInPlan;
			}
			set
			{
				var da = GetService<IOptionsDataAccess>();
				da.ShowPensumInPlan = value;
				Mediator.NotifyColleagues("SettingsRefresh");
			}
		}

		public bool ColorWeekendInPlan
		{
			get
			{
				var da = GetService<IOptionsDataAccess>();
				return da.ColorWeekendInPlan;
			}
			set
			{
				var da = GetService<IOptionsDataAccess>();
				da.ColorWeekendInPlan = value;
				Mediator.NotifyColleagues("SettingsRefresh");
			}
		}

        public bool ShowPrsId
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.ShowPrsId;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.ShowPrsId = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        IList<string> _nameDisplayOptions;
        public IList<string> NameDisplayOptions
        {
            get
            {
                if (_nameDisplayOptions == null)
                {
                    _nameDisplayOptions = new List<string>
                    {
                        Properties.Resources.PrintVornameName,
                        Properties.Resources.PrintNameVorname,
                        Properties.Resources.PrintNameV,
                        Properties.Resources.PrintName
                    };
                }
                return _nameDisplayOptions;
            }
        }

        public string SelectedNameDisplayOption
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return NameDisplayOptions[(int)da.NameDisplayOption];
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.NameDisplayOption = (enNameDisplay)NameDisplayOptions.IndexOf(value);
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

		#endregion

		#region Colors

		public Color DienstCountPlusColor
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.DienstCountPlusColor;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.DienstCountPlusColor = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public Color DienstCountEnoughColor
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.DienstCountEnoughColor;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.DienstCountEnoughColor = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public Color DienstCountMinusColor
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.DienstCountMinusColor;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.DienstCountMinusColor = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public Color SaturdayColor
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.SaturdayColor;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.SaturdayColor = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public Color SundayColor
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.SundayColor;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.SundayColor = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
		}

		public Color HolidayColor
		{
			get
			{
				var da = GetService<IOptionsDataAccess>();
				return da.HolidayColor;
			}
			set
			{
				var da = GetService<IOptionsDataAccess>();
				da.HolidayColor = value;
				Mediator.NotifyColleagues("SettingsRefresh");
			}
		}

		#endregion

		#region Work time settings

		/*
		numStdPTag.Value = (int)Option.NumberValue("Arbeitszeitprotag", 0);
			numStdPWoche.Value = (int)Option.NumberValue("Arbeitszeitprowoche", 0);
			numFerienPJahr.Value = (int)Option.NumberValue("Ferientage", 0);

			bool check = false;
			if ((int)Option.NumberValue("PrintOptions", 1) == 1) { check = true; } else { check = false; }
			chekcShowDif.Checked = check;
			if ((int)Option.NumberValue("PrintOptions", 2) == 1) { check = true; } else { check = false; }
			checkShowSoll.Checked = check;
			if ((int)Option.NumberValue("PrintOptions", 3) == 1) { check = true; } else { check = false; }
			checkShowHaben.Checked = check;
		 */

		public double WorkTimePerDay
		{
			get
			{
				var da = GetService<IOptionsDataAccess>();
				return da.WorkTimePerDay;
			}
			set
			{
				var da = GetService<IOptionsDataAccess>();
				da.WorkTimePerDay = value;
				Mediator.NotifyColleagues("SettingsRefresh");
			}
		}

		public double WorkTimePerWeek
		{
			get
			{
				var da = GetService<IOptionsDataAccess>();
				return da.WorkTimePerWeek;
			}
			set
			{
				var da = GetService<IOptionsDataAccess>();
				da.WorkTimePerWeek = value;
				Mediator.NotifyColleagues("SettingsRefresh");
			}
		}

		public double HolydaysPerYear
		{
			get
			{
				var da = GetService<IOptionsDataAccess>();
				return da.HolydaysPerYear;
			}
			set
			{
				var da = GetService<IOptionsDataAccess>();
				da.HolydaysPerYear = value;
				Mediator.NotifyColleagues("SettingsRefresh");
			}
		}

		#endregion

        #region Printing

        public bool PrintSaldoDifference
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.PrintSaldoDifference;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.PrintSaldoDifference = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public bool PrintSoll
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.PrintSoll;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.PrintSoll = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public bool PrintHaben
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.PrintHaben;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.PrintHaben = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        #endregion

        #region Person Overview

        //if ((int)Option.NumberValue("PersonDisplaOptions", 1) == 1) { check = true; } else { check = false; }
        //    checkShowAbteilungsName.Checked = check;
        public bool ShowAbteilungName
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.ShowAbteilungName;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.ShowAbteilungName = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        //    if ((int)Option.NumberValue("PersonDisplaOptions", 2) == 1) { check = true; } else { check = false; }
        //    checkShowMonth.Checked = check;
        public bool ShowMonth
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.ShowMonth;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.ShowMonth = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        //    if ((int)Option.NumberValue("PersonDisplaOptions", 3) == 1) { check = true; } else { check = false; }
        //    checkShowSaldo.Checked = check;
        public bool ShowSaldo
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.ShowSaldo;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.ShowSaldo = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        //    if ((int)Option.NumberValue("PersonDisplaOptions", 4) == 1) { check = true; } else { check = false; }
        //    checkPlanName.Checked = check;
        public bool ShowPlanName
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.ShowPlanName;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.ShowPlanName = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        #endregion

        #region General

        string _activeManid;
        IList<Mandant> _mandanten;
        public IList<Mandant> Mandanten
        {
            get
            {
                if (_mandanten == null)
                {
                    var da = GetService<IOptionsDataAccess>();
                    var lst = da.GetMandanten();
                    _mandanten = lst != null ? new ObservableCollection<Mandant>(lst) : new ObservableCollection<Mandant>();

                    _activeManid = da.GetActiveMandid();
                    if (!string.IsNullOrEmpty(_activeManid))
                        SelectedMandant = (from a in _mandanten where a.MandId == _activeManid select a).FirstOrDefault();
                }
                return _mandanten;
            }
        }

        Mandant _selectedMandant;
        public Mandant SelectedMandant
        {
            get
            {
                return _selectedMandant;
            }
            set
            {
                _selectedMandant = value;
                RaisePropertyChanged("SelectedMandant");
            }
        }

        ICommand _activateMandantCommand;
        public ICommand ActivateMandantCommand
        {
            get
            {
                if (_activateMandantCommand == null)
                    _activateMandantCommand = new RelayCommand(OnExecuteActivateMandant, prp => SelectedMandant != null && SelectedMandant.MandId != _activeManid);
                return _activateMandantCommand;
            }
        }

        private void OnExecuteActivateMandant(object param)
        {
            var da = GetService<IOptionsDataAccess>();
            if (da.SetActiveMandant(SelectedMandant.MandId))
                _activeManid = SelectedMandant.MandId;

        }

        public bool SavePassword
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.SavePassword;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.SavePassword = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }


        public bool ShowDatabaseName
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.ShowDatabaseName;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.ShowDatabaseName = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public bool DeleteSchemaOnDeletePlan
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.DeleteSchemaOnDeletePlan;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.DeleteSchemaOnDeletePlan = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        public bool ResetLastSchemaOnDeleteSchema
        {
            get
            {
                var da = GetService<IOptionsDataAccess>();
                return da.ResetLastSchemaOnDeleteSchema;
            }
            set
            {
                var da = GetService<IOptionsDataAccess>();
                da.ResetLastSchemaOnDeleteSchema = value;
                Mediator.NotifyColleagues("SettingsRefresh");
            }
        }

        #endregion
    }
}
